package com.ophiux.cs.api.consultBase.impl;

import com.google.gson.reflect.TypeToken;
import com.ophiux.cs.api.consultBase.port.IDeptApiPort;
import com.ophiux.cs.application.consultBase.dto.*;
import com.ophiux.cs.application.consultBase.service.*;
import com.ophiux.cs.application.core.AppResultCode;
import com.ophiux.cs.application.core.Request;
import com.ophiux.cs.application.core.ResponsePage;
import com.ophiux.cs.application.core.Result;
import com.ophiux.utils.parser.json.JsonUtils;
import com.ophiux.utils.parser.json.JsonUtils.TypeObtainCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.List;

@Service
@Slf4j
public class DeptApiService implements IDeptApiPort {
	@Autowired
    IAddDeptAppService addDeptAppService;
    @Autowired
    IUpdateDeptAppService updateDeptAppService;
    @Autowired
    IDeleteDeptAppService deleteDeptAppService;
    @Autowired
    IGetDeptAppService getDeptAppService;

    @Override
	public String createDepartMent(String data) {
		log.info("createDepartMent接口,请求参数："+data);
        Request<AddDeptRequest> request = null;
		Result<AddDeptResponse> result = null;
        try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				@Override
				public Type getObtainType() {
					return new TypeToken<Request<AddDeptRequest>>() {
					}.getType();
				}
			});
		} catch (Exception e) {
			log.info("createDepartMent接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}

        AddDeptRequest dto =request.getPARAM();
		result = addDeptAppService.createDept(dto);

		String returnMessage = JsonUtils.toJson(result);
		log.info("createHospital接口,返回参数："+returnMessage);
		return returnMessage;
	}

    @Override
    public String updateDept(String data) {
        log.info("updateDoctor接口,请求参数："+data);
        Request<UpdateDeptRequest> request = null;
        Result<UpdateDeptResponse> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<UpdateDeptRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("updateHospital接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        UpdateDeptRequest dto =request.getPARAM();
        result = updateDeptAppService.updateDept(dto);
        String returnMessage = JsonUtils.toJson(result);
        log.info("updateHospital接口,返回参数："+returnMessage);
        return returnMessage;
    }

    @Override
    public String deleteDept(String data) {
        log.info("deleteDoctor接口,请求参数："+data);
        Request<DeleteDeptRequest> request = null;
        Result<DeleteDeptResponse> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<DeleteDeptRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("deleteHospital接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        DeleteDeptRequest dto =request.getPARAM();
        result = deleteDeptAppService.deleteDept(dto);

        String returnMessage = JsonUtils.toJson(result);
        log.info("deleteDoctor接口,返回参数："+returnMessage);
        return returnMessage;
    }

    @Override
    public String getDept(String data) {
        log.info("【getDept】接口,请求参数："+data);
        Request<GetDeptRequest> request = null;
        Result<ResponsePage<GetDeptResponse>> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<GetDeptRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("【getDept】接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        GetDeptRequest dto =request.getPARAM();
        result = getDeptAppService.getDept(dto);

        String returnMessage = JsonUtils.toJson(result);
        log.info("【getDept】接口,返回参数："+returnMessage);
        return returnMessage;
    }

}
